/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CardGateway;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 *
 * @author dhaval
 */
public class GatewayClient extends ClientServer{
    public Socket socket;

    /**
     * GatewayClient() constructor creates a client object given the URL and port
     * number of a server that this client will connect to
     *
     * @param url -- a String giving the server's URL
     * @param url -- an int giving the server's port number
     */
    public GatewayClient(String url, int port) {
        try {
            socket = new Socket(url, port);
            System.out.println("CLIENT: connected to " + url + ":" + port);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    } // GatewayClient()

    /**
     * run() defines the client thread's main behavior which is simply to
     * request service from its server. Since an I/O exception may result, it is
     * handled here.
     */
    public void run() {
        try {
            requestService(socket);
            socket.close();
            System.out.println("CLIENT: connection closed");
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    } // run()

    /**
     * requestService() implements the details of the service request. In this
     * case it accepts a line of input from the user and passes it along to the
     * server. The protocol with the server requires that the server say "Hello"
     * first.
     *
     * @param socket -- the Socket connection to the server
     */
    protected void requestService(Socket socket) throws IOException {
        String servStr = readFromSocket(socket);          // Check for "Hello"
        System.out.println("SERVER: " + servStr);         // Report the server's response
        //System.out.println("CLIENT: type a line or 'goodbye' to quit"); // Prompt the user
        if (servStr.substring(0, 5).equals("Hello")) {
            String userStr = "";

            userStr = readFromKeyboard();                   // Get input from user
            writeToSocket(socket, userStr + "\n");          // Send it to server
            servStr = readFromSocket(socket);               // Read the server's response
            System.out.println("SERVER: " + servStr);       // Report the server's response
        }
    } // requestService()

    /**
     * readFromKeyboard() reads a line of input from the keyboard
     */
    protected String readFromKeyboard() throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("INPUT: ");
        String line = input.readLine();
        return line;
    } // readFromKeyboard(
    public static void main(String args[]) {
        GatewayClient client = new GatewayClient("localhost", 10002);
        client.start();
    }
}
